package com.jwpdk.dbframework;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.log4j.Logger;
/**
 * 
 * @author 王志伟
 *
 */
public class ParameterStatement implements IStatement {

	protected final Logger		log	= Logger.getLogger(this.getClass());
	private PreparedStatement	statement;
	private String				sqlstr;
	private SQL					param;
	private SQL[]				params;
	private Connection			conn;

	/**
	 * construct function
	 * @param conn
	 * @param sqlstr
	 * @throws Exception
	 */
	public ParameterStatement(Connection conn, SQL sql) throws Exception {
		this.conn = conn;
		this.sqlstr = sql.getAskSql();
		this.param = sql;
		statement = this.conn.prepareStatement(sqlstr);
	}

	/**
	 * construct function
	 * @param conn
	 * @param sql
	 * @param params
	 * @throws Exception
	 */
	public ParameterStatement(Connection conn, SQL[] sqls) throws Exception {
		this.conn = conn;
		for (int i = 0; i < sqls.length; i++) {
			this.sqlstr = sqls[i].getAskSql();
		}
		this.params = sqls;
		statement = this.conn.prepareStatement(sqlstr);
	}

	/**
	 * get sql
	 * @return String
	 * @throws Exception
	 */
	public String getSql() throws Exception {
		return sqlstr;
	}

	/**
	 * get statement
	 * @return Statement
	 * @throws Exception
	 */
	public Statement getStatement() throws Exception {
		return statement;
	}

	/**
	 * get Connection
	 * @return Statement
	 * @throws Exception
	 */
	public Connection getConnection() throws Exception {
		return conn;
	}

	/**
	 * execute query
	 * @return ResultSet
	 * @throws Exception
	 */
	public ResultSet executeQuery() throws Exception {
		param.fillParams(statement);
		try {
			long beginTime = System.currentTimeMillis();
			log.debug("使用链接：" + conn);
			ResultSet result = statement.executeQuery();
			DAOBase.getExecuteTimeBySql(log,beginTime);
			return result;
		} catch ( java.sql.SQLException e ) {
			log.error("ERROR SQL： " + sqlstr);
			throw e;
		}
	}

	/**
	 * execute update
	 * @return int
	 * @throws Exception
	 */
	public int executeUpdate() throws Exception {
		param.fillParams(statement);
		try {
			long beginTime = System.currentTimeMillis();
			int result = statement.executeUpdate();
			DAOBase.getExecuteTimeBySql(log,beginTime);
			return result;
		} catch ( java.sql.SQLException e ) {
			log.error("ERROR SQL：" + sqlstr);
			throw e;
		}
	}

	/**
	 * execute batch
	 * @return int[]
	 * @throws Exception
	 */
	public int[] executeBatch() throws Exception {
		if ( params.length == 0 )
			return new int[] {};

		for (int i = 0; i < params.length; i++) {
			params[i].fillParams(statement);
			log.debug("Batch PARAM：" + params[i]);
			statement.addBatch();
		}
		try {
			long beginTime = System.currentTimeMillis();
			int[] result = statement.executeBatch();
			DAOBase.getExecuteTimeBySql(log,beginTime);
			return result;
		} catch ( java.sql.SQLException e ) {
			log.error("ERROR Batch SQL：" + sqlstr);
			throw e;
		}
	}

	/**
	 * close statement
	 * @throws Exception
	 */
	public void close() throws Exception {
		if ( statement != null )
			statement.close();
	}
}